package com.example.myapplication.hell

//入口
fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)

// 过滤 - 找出偶数
    val evens = numbers.filter { it % 2 == 0 }  // [2, 4]

// 映射 - 每个元素加倍
    val doubled = numbers.map { it * 2 }  // [2, 4, 6, 8, 10]

// 查找 - 找到第一个大于3的元素
    val firstLarge = numbers.first { it > 3 }  // 4

    println("$evens, $doubled, $firstLarge")

// 遍历 - 打印元素
    numbers.forEach { println("Number: $it") }
    // 定义一个函数类型的参数operation是什么
    // 它接收两个Int类型的参数，返回一个Int类型的结果
    fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
        return operation(a, b)
    }
    // 调用calculate函数
    val res1 = calculate(2, 3) { a, b -> a + b }
    val res2 = calculate(2, 3) { a, b -> a * b }
    println("res1: $res1, res2: $res2")
    // 1. if语句
    fun ifFunc(num1: Int, num2: Int): Int {
        // 写法1:
        //    if(num1 > num2){
        //        return num1
        //    } else {
        //        return num2
        //    }

        // 写法2:
        return if (num1 > num2) num1 else num2
    }
    println(ifFunc(100, 50)) // 100
    //分割线
    println("-----------------")
    println(whereFunc1(10))
    println(whereFunc2(85))
}

fun whereFunc1(num1: Int): String {
    val res = when (num1) {
        1 -> "one"
        2 -> "two"
        3 -> "three"
        else -> "other"
    }
    return res
}

fun whereFunc2(score: Int): String = when (score) {
    in 90..100 -> "A"
    in 80 until 90 -> "B"
    in 70 until 80 -> "C"
    in 60 until 70 -> "D"
    else -> "F"
}
